package com.oig.sys.log.controller;


import com.oig.common.module.SysPageInfo;
import com.oig.common.module.SysRequest;
import com.oig.common.validator.GroupAdd;
import com.oig.sys.log.dto.SysLogDto;
import com.oig.sys.log.service.ITbSysLogService;
import com.oig.sys.log.vo.SysLogVo;
import com.oig.sys.security.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "操作日志管理")
@RestController
@RequestMapping("/syslog")
@Slf4j
public class TbSysLogController {

    private final ITbSysLogService sysLogService;

    @Autowired
    public TbSysLogController(ITbSysLogService sysLogService){
        this.sysLogService = sysLogService;
    }

    @Hidden
    @Operation(summary = "保存日志")
    @PostMapping(value = "/addSysLog")
    public void addSysLog(@RequestBody @Validated(GroupAdd.class) SysLogDto logDto){
        log.info("add sysLog : {}", logDto);
        sysLogService.addSysLog(logDto);
    }

    @Operation(summary = "查看访问日志")
    @PostMapping(value = "/info")
    public SysPageInfo<SysLogVo> info(@RequestBody SysRequest<SysLogDto> logDto){
        log.info("get sys Log info: {}", logDto);
        if (SecurityUtil.getDefineUserMain().getTenantId()!=0){
            if (logDto.getParamDto()==null){
                logDto.setParamDto(new SysLogDto());
            }
            if (logDto.getParamDto().getTenantId()==null){
                logDto.getParamDto().setTenantId(SecurityUtil.getDefineUserMain().getTenantId());
            }
        }
        return sysLogService.getSysLogInfo(logDto);
    }

    @Operation(summary = "根据id查看日志")
    @PostMapping(value = "/info/{id}")
    public SysLogVo info(@PathVariable("id") String id) {
        log.info("get sysLog info by id: {}", id);
        return sysLogService.getSysLogInfoById(id);
    }

}
